﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DiskFactory : MonoBehaviour
{
    public GameObject diskPrefab = null;
    private List<DiskData> used = new List<DiskData>();
    private List<DiskData> free = new List<DiskData>();

    public GameObject GetDisk(int round)
    {
        int diskProbability = 0;
        int range1 = 4, range2 = 7, range3 = 10;
        string tag;
        diskPrefab = null;

        if (round == 1)
        {
            diskProbability = Random.Range(0, range1);
        }
        else if (round == 2)
        {
            diskProbability = Random.Range(0, range2);
        }
        else
        {
            diskProbability = Random.Range(0, range3);
        }

        if (diskProbability <= range1)
        {
            tag = "disk1";
        }
        else if (diskProbability <= range2 && diskProbability > range1)
        {
            tag = "disk2";
        }
        else
        {
            tag = "disk3";
        }

        for (int i = 0; i < free.Count; i++)
        {
            if (free[i].tag == tag)
            {
                diskPrefab = free[i].gameObject;
                free.Remove(free[i]);
                break;
            }
        }

        if (diskPrefab == null)
        {
            if (tag == "disk1")
            {
                diskPrefab = Object.Instantiate(Resources.Load<GameObject>("Prefabs/disk1"), new Vector3(0, -10F, 0), Quaternion.identity, null) as GameObject;
                diskPrefab.GetComponent<DiskData>().speed = 10F;
                diskPrefab.GetComponent<DiskData>().score = 1;
            }
            else if (tag == "disk2")
            {
                diskPrefab = Object.Instantiate(Resources.Load<GameObject>("Prefabs/disk2"), new Vector3(0, -10F, 0), Quaternion.identity, null) as GameObject;
                diskPrefab.GetComponent<DiskData>().speed = 8F;
                diskPrefab.GetComponent<DiskData>().score = 3;
            }
            else
            {
                diskPrefab = Object.Instantiate(Resources.Load<GameObject>("Prefabs/disk3"), new Vector3(0, -10F, 0), Quaternion.identity, null) as GameObject;
                diskPrefab.GetComponent<DiskData>().speed = 14F;
                diskPrefab.GetComponent<DiskData>().score = 5;
            }
            diskPrefab.GetComponent<DiskData>().size=diskPrefab.transform.localScale;
            diskPrefab.GetComponent<DiskData>().color = diskPrefab.GetComponent<Renderer>().material.color;
            diskPrefab.GetComponent<DiskData>().direction = new Vector3(10F, 10F, 0);
        }

        used.Add(diskPrefab.GetComponent<DiskData>());
        return diskPrefab;
    }

    public void FreeDisk(GameObject disk)
    {
        for (int i = 0; i < used.Count; i++)
        {
            if (disk.GetInstanceID() == used[i].gameObject.GetInstanceID())
            {
                used[i].gameObject.SetActive(false);
                free.Add(used[i]);
                used.Remove(used[i]);
                break;
            }
        }
    }
}
